﻿using System;
using System.IO;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;
using visinia.SmartEngine.Site;
using viWiki.Classes;
using viWiki.Controllers;

public partial class Modules_viWiki_WikiPageView : WebModule
{
    protected void Page_Load(object sender, EventArgs e)
    {
        WebPageController.AddCssLink(Page, ResolveUrl("Wiki.css"), true);
        int pageid = 0;
        if (Request.QueryString["WikiPageId"] == null)
            pageid = WikiController.WikiMainPageId;
        else
            Int32.TryParse(Request.QueryString["WikiPageId"], out pageid);

        WikiPage wikipage = WikiPageController.GetWikiPage(pageid);
        if (wikipage == null)
            return;        
        PageView view = (PageView)LoadControl(GetWikiPageView());
        view.WikiPage = wikipage;
        WikiPage.Controls.Add(view);
        // SEO            
        WebPageController.AddTitle(Page, wikipage.Name);
        WebPageController.AddMetaTag(Page, WebPageController.MetaTag.Description, WebHelpers.StripHtml(wikipage.Content.Substring(0, wikipage.Content.Length < 100 ? wikipage.Content.Length : 100)));
        WebPageController.AddMetaTag(Page, WebPageController.MetaTag.Author, wikipage.Author);
        // keywords
        //WebPageController.AddMetaTag(Page, WebPageController.MetaTag.Keywords, keywords);     
    }

    private string GetWikiPageView()
    {
        // search for the viWiki.PageView.ascx
        // 1 search in the current active theme folder
        //   if not found then
        // 2 search in the current folder

        string currentThemeDir = Server.MapPath(WebRoots.GetThemesRoot) + SiteSettings.Theme;
        string file = currentThemeDir + "\\viWiki.PageView.ascx";
        if (File.Exists(file))
            return WebRoots.GetThemesRoot + SiteSettings.Theme + "/viWiki.PageView.ascx";
        else
            return "viWiki.PageView.ascx";
    }
}
